Skip to content

Conversation

HerringtonDarkholme
Copy link
Member

Fix #994

Based on #1121

Vuex has added $store: Store<any> to Vue instance. This is suboptimal since it doesn't allow users to provide their own store type.

By utilizing TS2.8's conditional type, we can write a PermissiveAny<T> type which allows users to customize interface with any as fall-back. It can provide opt-in type safety for store.

The store type is parameterized by State. By introducing a global RootState interface, we can annotate $store as Store<PermissiveAny<RootState>>. If users don't augment RootState, the $store type is equivalent to Store<any>. On the other hand, if users do augment RootState, the store type will be changed to Store<RootState>.

Effectively, this feature allows users to manual annotate store type, but without breaking existing code.

cc @blake-newman @ktsn

ktsn and others added 27 commits November 6, 2017 12:10
@HerringtonDarkholme
Copy link
Member Author

HerringtonDarkholme commented Mar 17, 2018

There are some concerns:

  1. should we also apply this approach to ComponentOption? It seems not desirable as Feature: Manually install vuex/types/vue.d.ts #994 (comment)

  2. This augmentation is global. That is, once declared, every $store is typed exactly the same as declaration. This might have unexpected outcome when users have multiple store instantiation. For example, test is not included in this pull request because we have two test examples. One global type prevents us to write two distinct store in one compilation.

  3. Store is now only parameterized by State. If this approach is adopted, should we add more type parameters to store?

@ktsn ktsn added the types Related to typings only label Mar 19, 2018
@Koc
Copy link

Koc commented Jan 19, 2019

@HerringtonDarkholme have you looked at https://github.com/championswimmer/vuex-module-decorators ? Decorators allows us write store modules as simple classes. It is very useful and less verbose.

@kiaking
Copy link
Member

kiaking commented Apr 21, 2020

Closing this PR since this is technically breaking change, we can't introduce this on Vuex 3. However, the fix will be applied to Vuex 4 via #1717.

@kiaking kiaking closed this Apr 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

types Related to typings only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Manually install vuex/types/vue.d.ts

4 participants